package com.tank.collision;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.dsl.components.HealthIntComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.tank.constant.Config;
import com.tank.constant.GameType;
import com.tank.effect.HelmetEffect;
import com.tank.effect.ShipEffect;

public class BulletEnemyCollideHandler extends CollisionHandler {
    public BulletEnemyCollideHandler() {
        super(GameType.BULLET, GameType.ENEMY);
    }

    @Override
    protected void onCollisionBegin(Entity bullet, Entity enemy) {
        GameType ownerType = bullet.getObject(Config.OWNER_TYPE);
        if (ownerType == GameType.ENEMY) {
            return;
        }
        bullet.removeFromWorld();
        boolean hasHelmet = enemy.getComponent(EffectComponent.class).hasEffect(HelmetEffect.class);
        if (!hasHelmet) {
            if (enemy.getComponent(EffectComponent.class).hasEffect(ShipEffect.class)) {
                //remove ship effect
                enemy.getComponent(EffectComponent.class).endEffect(ShipEffect.class);
            } else {
                FXGL.spawn("mildExplosion", bullet.getCenter().subtract(50 / 2.0, 50 / 2.0));
                enemy.getComponent(HealthIntComponent.class).damage(bullet.getInt(Config.BULLET_LEVEL_KEY) + 1);
            }
        }
    }
}
